home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / idlelib / GrepDialog.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  5KB  |  171 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import os
  5. import fnmatch
  6. import sys
  7. from Tkinter import *
  8. import SearchEngine
  9. from SearchDialogBase import SearchDialogBase
  10.  
  11. def grep(text, io = None, flist = None):
  12.     root = text._root()
  13.     engine = SearchEngine.get(root)
  14.     if not hasattr(engine, '_grepdialog'):
  15.         engine._grepdialog = GrepDialog(root, engine, flist)
  16.     
  17.     dialog = engine._grepdialog
  18.     searchphrase = text.get('sel.first', 'sel.last')
  19.     dialog.open(text, searchphrase, io)
  20.  
  21.  
  22. class GrepDialog(SearchDialogBase):
  23.     title = 'Find in Files Dialog'
  24.     icon = 'Grep'
  25.     needwrapbutton = 0
  26.     
  27.     def __init__(self, root, engine, flist):
  28.         SearchDialogBase.__init__(self, root, engine)
  29.         self.flist = flist
  30.         self.globvar = StringVar(root)
  31.         self.recvar = BooleanVar(root)
  32.  
  33.     
  34.     def open(self, text, searchphrase, io = None):
  35.         SearchDialogBase.open(self, text, searchphrase)
  36.         if io:
  37.             if not io.filename:
  38.                 pass
  39.             path = ''
  40.         else:
  41.             path = ''
  42.         (dir, base) = os.path.split(path)
  43.         (head, tail) = os.path.splitext(base)
  44.         if not tail:
  45.             tail = '.py'
  46.         
  47.         self.globvar.set(os.path.join(dir, '*' + tail))
  48.  
  49.     
  50.     def create_entries(self):
  51.         SearchDialogBase.create_entries(self)
  52.         self.globent = self.make_entry('In files:', self.globvar)
  53.  
  54.     
  55.     def create_other_buttons(self):
  56.         f = self.make_frame()
  57.         btn = Checkbutton(f, anchor = 'w', variable = self.recvar, text = 'Recurse down subdirectories')
  58.         btn.pack(side = 'top', fill = 'both')
  59.         btn.select()
  60.  
  61.     
  62.     def create_command_buttons(self):
  63.         SearchDialogBase.create_command_buttons(self)
  64.         self.make_button('Search Files', self.default_command, 1)
  65.  
  66.     
  67.     def default_command(self, event = None):
  68.         prog = self.engine.getprog()
  69.         if not prog:
  70.             return None
  71.         
  72.         path = self.globvar.get()
  73.         if not path:
  74.             self.top.bell()
  75.             return None
  76.         
  77.         OutputWindow = OutputWindow
  78.         import OutputWindow
  79.         save = sys.stdout
  80.         
  81.         try:
  82.             sys.stdout = OutputWindow(self.flist)
  83.             self.grep_it(prog, path)
  84.         finally:
  85.             sys.stdout = save
  86.  
  87.  
  88.     
  89.     def grep_it(self, prog, path):
  90.         (dir, base) = os.path.split(path)
  91.         list = self.findfiles(dir, base, self.recvar.get())
  92.         list.sort()
  93.         self.close()
  94.         pat = self.engine.getpat()
  95.         print 'Searching %r in %s ...' % (pat, path)
  96.         hits = 0
  97.         for fn in list:
  98.             
  99.             try:
  100.                 f = open(fn)
  101.             except IOError:
  102.                 msg = None
  103.                 print msg
  104.                 continue
  105.  
  106.             lineno = 0
  107.             while None:
  108.                 block = f.readlines(100000)
  109.                 if not block:
  110.                     break
  111.                 
  112.                 for line in block:
  113.                     lineno = lineno + 1
  114.                     if line[-1:] == '\n':
  115.                         line = line[:-1]
  116.                     
  117.                     if prog.search(line):
  118.                         sys.stdout.write('%s: %s: %s\n' % (fn, lineno, line))
  119.                         hits = hits + 1
  120.                         continue
  121.                 
  122.         
  123.         if hits:
  124.             if hits == 1:
  125.                 s = ''
  126.             else:
  127.                 s = 's'
  128.             print 'Found', hits, 'hit%s.' % s
  129.             print '(Hint: right-click to open locations.)'
  130.         else:
  131.             print 'No hits.'
  132.  
  133.     
  134.     def findfiles(self, dir, base, rec):
  135.         
  136.         try:
  137.             if not dir:
  138.                 pass
  139.             names = os.listdir(os.curdir)
  140.         except os.error:
  141.             msg = None
  142.             print msg
  143.             return []
  144.  
  145.         list = []
  146.         subdirs = []
  147.         for name in names:
  148.             fn = os.path.join(dir, name)
  149.             if os.path.isdir(fn):
  150.                 subdirs.append(fn)
  151.                 continue
  152.             if fnmatch.fnmatch(name, base):
  153.                 list.append(fn)
  154.                 continue
  155.         
  156.         if rec:
  157.             for subdir in subdirs:
  158.                 list.extend(self.findfiles(subdir, base, rec))
  159.             
  160.         
  161.         return list
  162.  
  163.     
  164.     def close(self, event = None):
  165.         if self.top:
  166.             self.top.grab_release()
  167.             self.top.withdraw()
  168.         
  169.  
  170.  
  171.